package com.seaboxdata.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;


/**
 * WSDL调用工具类
 *
 * @Author wei
 * @Date 2021/8/16 11:11
 **/
public class WsdlUtil {

    public static void main(String[] args) throws Exception {
        String endpoint = "http://10.253.101.227/api/gateway/api/xchgshrApi/webservices/services/queryResService?wsdl";
        String namespace = "http://webservice.smartcity.pingan.com";
        String method = "queryData";
        String soap = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <AppRequest><RequestID>SZ0112340010820210812101216307236</RequestID><AppId>SZ01123400108</AppId><AppRequestType>APPREQUEST_FETCHRESDATA</AppRequestType><ConditionObject><ResCode>15b8e433048c4f33a49b25f2cca9934e</ResCode><OrgId>SZ011300</OrgId><ResName>广东省新冠病毒检测信息（未出结果）</ResName><IsAllSelect>false</IsAllSelect><ResIndexs><ResIndex><ResIndexName>KFGD_ID</ResIndexName><ResInexCode>KFGD_ID</ResInexCode></ResIndex><ResIndex><ResIndexName>KFGD_CTIME</ResIndexName><ResInexCode>KFGD_CTIME</ResInexCode></ResIndex><ResIndex><ResIndexName>KFGD_DEL_FLAG</ResIndexName><ResInexCode>KFGD_DEL_FLAG</ResInexCode></ResIndex><ResIndex><ResIndexName>XM</ResIndexName><ResInexCode>XM</ResInexCode></ResIndex><ResIndex><ResIndexName>GJ</ResIndexName><ResInexCode>GJ</ResInexCode></ResIndex><ResIndex><ResIndexName>ZJLX</ResIndexName><ResInexCode>ZJLX</ResInexCode></ResIndex><ResIndex><ResIndexName>ZJHM</ResIndexName><ResInexCode>ZJHM</ResInexCode></ResIndex><ResIndex><ResIndexName>CYRQ</ResIndexName><ResInexCode>CYRQ</ResInexCode></ResIndex><ResIndex><ResIndexName>JCSJ</ResIndexName><ResInexCode>JCSJ</ResInexCode></ResIndex><ResIndex><ResIndexName>JCJG</ResIndexName><ResInexCode>JCJG</ResInexCode></ResIndex><ResIndex><ResIndexName>JCJIEGUO</ResIndexName><ResInexCode>JCJIEGUO</ResInexCode></ResIndex><ResIndex><ResIndexName>BYZD2</ResIndexName><ResInexCode>BYZD2</ResInexCode></ResIndex><ResIndex><ResIndexName>BYZD4</ResIndexName><ResInexCode>BYZD4</ResInexCode></ResIndex><ResIndex><ResIndexName>BYZD5</ResIndexName><ResInexCode>BYZD5</ResInexCode></ResIndex><ResIndex><ResIndexName>NL</ResIndexName><ResInexCode>NL</ResInexCode></ResIndex><ResIndex><ResIndexName>SJHM</ResIndexName><ResInexCode>SJHM</ResInexCode></ResIndex><ResIndex><ResIndexName>XB</ResIndexName><ResInexCode>XB</ResInexCode></ResIndex><ResIndex><ResIndexName>HJD</ResIndexName><ResInexCode>HJD</ResInexCode></ResIndex><ResIndex><ResIndexName>JZD</ResIndexName><ResInexCode>JZD</ResInexCode></ResIndex><ResIndex><ResIndexName>RYLY</ResIndexName><ResInexCode>RYLY</ResInexCode></ResIndex><ResIndex><ResIndexName>RYSF</ResIndexName><ResInexCode>RYSF</ResInexCode></ResIndex><ResIndex><ResIndexName>JWRJRY</ResIndexName><ResInexCode>JWRJRY</ResInexCode></ResIndex><ResIndex><ResIndexName>JSLX</ResIndexName><ResInexCode>JSLX</ResInexCode></ResIndex><ResIndex><ResIndexName>BBLX</ResIndexName><ResInexCode>BBLX</ResInexCode></ResIndex><ResIndex><ResIndexName>JCXM</ResIndexName><ResInexCode>JCXM</ResInexCode></ResIndex><ResIndex><ResIndexName>CYDD</ResIndexName><ResInexCode>CYDD</ResInexCode></ResIndex><ResIndex><ResIndexName>XXDWMC</ResIndexName><ResInexCode>XXDWMC</ResInexCode></ResIndex><ResIndex><ResIndexName>CBLS</ResIndexName><ResInexCode>CBLS</ResInexCode></ResIndex><ResIndex><ResIndexName>CBXZ</ResIndexName><ResInexCode>CBXZ</ResInexCode></ResIndex><ResIndex><ResIndexName>CBD</ResIndexName><ResInexCode>CBD</ResInexCode></ResIndex><ResIndex><ResIndexName>BYZD3</ResIndexName><ResInexCode>BYZD3</ResInexCode></ResIndex><ResIndex><ResIndexName>YLWB2</ResIndexName><ResInexCode>YLWB2</ResInexCode></ResIndex><ResIndex><ResIndexName>CD_TIME</ResIndexName><ResInexCode>CD_TIME</ResInexCode></ResIndex></ResIndexs><Conditions><Condition><ResIndexName>XM</ResIndexName></Condition><Condition><ResIndexName>ZJHM</ResIndexName></Condition></Conditions></ConditionObject></AppRequest>";
        Map<String, Object> params = new HashMap<>();
        Document document = doWsdlBySoap(endpoint, namespace, method, soap, params);
        System.out.println(document);
    }

    /**
     * WSDL发送SOAP请求拿到返回数据
     *
     * @param endpoint
     * @param namespace
     * @param method
     * @param soap
     * @param params
     * @return
     * @throws Exception
     */
    public static Document doWsdlBySoap(String endpoint, String namespace, String method, String soap,
            Map<String, Object> params) throws Exception {
        InputStream iStream = null;
        OutputStream oStream = null;
        HttpURLConnection con = null;
        DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = bf.newDocumentBuilder();
        Document document = null;
        try {
            URL url = new URL(endpoint);
            con = (HttpURLConnection) url.openConnection();
            con.setDoInput(true);
            con.setDoOutput(true);
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            oStream = con.getOutputStream();
            //下面这行代码是用字符串拼出要发送的xml，xml的内容是从测试软件里拷贝出来的
            //需要注意的是，有些空格不要弄丢哦，要不然会报500错误的。
            //参数什么的，你可以封装一下方法，自动生成对应的xml脚本
            String paramStr = "";
            if (StringUtils.isEmpty(soap)) {
                soap = "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                        "    <Body>" +
                        "        <" + method + " xmlns=\"" + namespace + "\">";
                for (Map.Entry<String, Object> entry : params.entrySet()) {
                    paramStr += "<" + entry.getKey() + " xmlns=\"\">" + entry.getValue() + "</" + entry.getKey() + ">";
                }
                soap += paramStr;
                soap += "        </" + method + ">" +
                        "    </Body>" +
                        "</Envelope>";
            }
            oStream.write(soap.getBytes());
            oStream.close();

            iStream = con.getInputStream();

            document = db.parse(iStream);

            //下面这行输出返回的xml到控制台，相关的解析操作大家自己动手喽。
            //如果想要简单的话，也可以用正则表达式取结果出来。
            //String res = document.getElementsByTagName_r("qqCheckOnlineResponse").item(0).getTextContent();
            //System.out.println(res);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (iStream != null) {
                    iStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (oStream != null) {
                    oStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (con != null) {
                con.disconnect();
            }
        }
        return document;
    }
}

